import threading
from utils.db_weibo_util import * 
import re
import time

class WriteWeiBoDb(threading.Thread):

    def __init__(self,uid,cards):
        threading.Thread.__init__(self)
        self.cards=cards
        self.uid=uid

    def run(self):
        weibos = []
        for card in self.cards:
            if 'mblog' not in card:
                continue
            mblog = card['mblog']
            mblog_id = mblog['id']
            user = mblog['user']
            screen_name = user['screen_name']
            created_at = time.strptime(mblog['created_at'], '%a %b %d %H:%M:%S %z %Y')
            dynamic_time = time.strftime('%Y-%m-%d %H:%M:%S', created_at)
            text = mblog['text']
            text = re.sub(r'<[^>]+>', '', text)
            content = mblog['raw_text'] if mblog.get('raw_text', None) is not None else text
            jump_url = card['scheme']
            weibos.append((self.uid,screen_name,mblog_id,content,jump_url,dynamic_time,False))
        add_weibos(self.uid,weibos)


